package 哈希表;

/**
 * @author: 大怪
 * @email: 962527441@qq.com
 */

import java.util.Arrays;
import java.util.List;

/**
 * 题目：
 * 给定一个 24 小时制（小时:分钟 "HH:MM"）的时间列表，找出列表中任意两个时间的最小时间差并以分钟数表示。
 *
 * 示例 1：
 *
 * 输入：timePoints = ["23:59","00:00"]
 * 输出：1
 */
public class 最小时间差 {
    class Solution {
        public int findMinDifference(List<String> timePoints) {
            int ans = 720;
            int[] sum = new int[timePoints.size()];
            for (int i = 0; i < timePoints.size(); i++) {
                int a = Integer.parseInt(timePoints.get(i).split(":")[0]);
                int b = Integer.parseInt(timePoints.get(i).split(":")[1]);
                sum[i] = a * 60 + b;
            }
            Arrays.sort(sum);
            int pre = sum[0];
            int first = sum[0];
            for (int i = 1; i < timePoints.size(); i++) {
                ans = Math.min(ans, Math.min(sum[i] - pre, 1440 - sum[i] + first));
                pre = sum[i];
            }
            return ans;
        }
    }
}
